<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/07/07 18:07:59 $)
// $RCSfile: report_ajaxreport.php,v $ : $Revision: 1.1 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

$_tickets = $_ticketidlist = array();

// Load all the tickets by this userid
$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."tickets` WHERE `userid` = '". intval($_GET["userid"]) ."' ORDER BY `ticketid` DESC;");
while ($dbCore->nextRecord())
{
	$_tickets[$dbCore->Record["ticketid"]] = $dbCore->Record;
	$_tickets[$dbCore->Record["ticketid"]]["posts"] = array();
	$_ticketidlist[] = $dbCore->Record["ticketid"];
}

// Get the posts
$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."ticketposts` WHERE `ticketid` IN (". buildIN($_ticketidlist) .") ORDER BY `ticketpostid` ASC;");
while ($dbCore->nextRecord())
{
	$_tickets[$dbCore->Record["ticketid"]]["posts"][$dbCore->Record["ticketpostid"]] = $dbCore->Record;
}

echo '<table border="0" cellpadding="3" cellspacing="1" width="100%" class="tborder"><tr class="tabletitlerow"><td align="left" width="120">'. $_SWIFT["language"]["f_ticketid"] .'</td><td align="left" width="">'. $_SWIFT["language"]["f_subject"] .'</td><td align="center" width="150">'. $_SWIFT["language"]["date"] .'</td><td align="center" width="110">'. $_SWIFT["language"]["f_ticketstatus"] .'</td><td align="center" width="110" nowrap>'. $_SWIFT["language"]["f_avgresponse"] .'</td><td align="center" width="110" nowrap>'. $_SWIFT["language"]["f_resolution"] .'</td><td align="center" width="110" nowrap>'. $_SWIFT["language"]["f_timeworked"] .'</td><td align="center" width="120">'. $_SWIFT["language"]["f_staffworked"] .'</td></tr>';
if (_is_array($_tickets))
{
	foreach ($_tickets as $key=>$val)
	{
		// Calculate Resolution Time
		if (empty($val["laststaffreplytime"]))
		{
			$_resolution = $_SWIFT["language"]["notreplied"];
		} else {
			$_resolution = strColorDate(($val["laststaffreplytime"]-$val["dateline"]), false);
		}

		// Calculate Avg Response Time
		$_avgresponse = 0;
		$_lastposttime = $val["dateline"];
		$_responsecount = 0;
		$_stafflist = array();
		$_stafflisttxt = "";
		foreach ($val["posts"] as $postkey=>$postval)
		{
//			echo "Parsing Post: ".$postval["ticketpostid"]."<BR />";
			if ($postval["creator"] == POST_CLIENT)
			{
//				echo "Setting Last Post..";
				$_lastposttime = $val["dateline"];
			} else if ($postval["creator"] == POST_STAFF && !empty($_lastposttime)) {
				$_avgresponse += ($postval["dateline"]-$_lastposttime);
				$_responsecount++;
				$_lastposttime = 0;
//				echo "1AVG RESPONSE: $_avgresponse<BR />1Response Count: $_responsecount";

				if (!in_array($postval["staffid"], $_stafflist) && !empty($postval["staffid"]))
				{
					$_stafflist[] = $postval["staffid"];
//					echo "StaffID: ".$postval["staffid"]."<BR />";
					$_stafflisttxt .= $_SWIFT["staffcache"][$postval["staffid"]]["fullname"]."<BR />";
				}
			}
		}

//		echo "AVG RESPONSE: $_avgresponse<BR />Response Count: $_responsecount";

		if (!empty($_avgresponse))
		{
			$_avgresponse = round($_avgresponse/$_responsecount);
		}
		$_avgresponsetxt = strColorDate($_avgresponse, false);

		echo '<tr class="'. getRowBG() .'"><td align="left"><a href="index.php?_m=tickets&_a=viewticket&ticketid='. intval($val["ticketid"]) .'" target="_blank">'. iif($_SWIFT["settings"]["t_eticketid"]=="seq",$val["ticketid"], $val["ticketmaskid"]) .'</a></td><td align="left"><a href="index.php?_m=tickets&_a=viewticket&ticketid='. intval($val["ticketid"]) .'" target="_blank">'. $dbCore->escape($val["subject"]) .'</a></td><td align="center">'. edate($_SWIFT["settings"]["dt_datetimeformat"], $val["dateline"]) .'</td><td align="center">'. '<font color=\''. $_SWIFT["statuscache"][$val["ticketstatusid"]]["statuscolor"] .'\'>'.htmlspecialchars($_SWIFT["statuscache"][$val["ticketstatusid"]]["title"]).'</font>' .'</td><td align="center">'. $_avgresponsetxt .'</td><td align="center">'. $_resolution .'</td><td align="center">'. strColorDate($val["timeworked"]) .'</td><td align="center">'. $_stafflisttxt .'</td></tr>';
	}
}
echo '</table><BR />';
?>